package com.example.shiyan2

import android.content.BroadcastReceiver
import android.content.Intent
import android.content.IntentFilter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.widget.Toast
import control.goodsAdapter
import kotlinx.android.synthetic.main.activity_main.*
import model.goods

class MainActivity : AppCompatActivity() {
    private val goodsList=ArrayList<goods>()
    private var receiver=HeadsetDetectReceiver()
    var intentFilter:IntentFilter = IntentFilter()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initgoods()
        val adapter=goodsAdapter(this,R.layout.phone_layout,goodsList)
        listView.adapter=adapter
        listView.setOnItemClickListener{_,_,position,id ->
            //val goodsid=goodsList[position]

            val intent =Intent(this,DetailActivity::class.java)
            intent.putExtra("pos",position)
            startActivity(intent)
        }
        receiver=HeadsetDetectReceiver()

        intentFilter.addAction(Intent.ACTION_HEADSET_PLUG)
        registerReceiver(receiver,intentFilter)
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(receiver)
    }

    private fun initgoods() {
            goodsList.add(
                goods(
                    R.drawable.airpods,
                    "Apple AirPods Pro 配MagSafe无线充电盒 主动降噪无线蓝牙耳机 适用iPhone/iPad/Apple Watch",
                    1499.00f,
                    "京东自营",
                    "12万好评 98%好评",
                    "Apple产品京东自营旗舰店"
                )
            )
            goodsList.add(
                goods(
                    R.drawable.huawei,
                    "HUAWEI P50 Pro 4G全网通 原色双影像单元 麒麟9000芯片 万象双环设计 8GB+256GB曜金黑华为手机",
                    6488.00f,
                    "分期免息",
                    "10万+评论 98%好评",
                    "华为官方旗舰店"
                )
            )
            goodsList.add(
                goods(
                    R.drawable.iphone13,
                    "Apple iPhone 13 Pro Max (A2644) 256GB 远峰蓝色 支持移动联通电信5G 双卡双待手机",
                    9799.00f,
                    "京东自营",
                    "20万+评论 速抢！",
                    "Apple产品京东自营旗舰店"
                )
            )
            goodsList.add(
                goods(
                    R.drawable.xiaomi,
                    "小米电视 A32 32英寸 立体声扬声器 64位处理器 享空间私享影音 智能网络教育电视 L32R6-A 红米Redmi 电视",
                    699.00f,
                    "线下同款",
                    "臻选好货，嗨购全场",
                    "小米官方旗舰店"
                )
            )
    }
}